package com.hq.ims.data.utils;

import com.hq.ims.data.entityenum.EnumPwdLevel;
import com.hq.utils.util.HanYuPinyinUtils;

import java.util.List;
import java.util.regex.Pattern;

/**
 * TODO
 * <p>
 * jbp
 */
public class PasswordUtils {
    /**
     * 验证密码是否通过正则
     *
     * @param pwd 密码明文
     */
    public static EnumPwdLevel validRegPwdRE(String pwd) {
        if (pwd.length() < 8) {
            return EnumPwdLevel.Low;
        }
        List<String> pwdRegList = LocalConstant.PWD_REG_LIST;
        int hitTheTargetCount = 0;
        boolean isContainSpecialSymbol = false;
        for (int i = 0; i < pwdRegList.size(); i++) {
            String reg = pwdRegList.get(i);
            boolean matches = Pattern.matches(reg, pwd);
            if (matches) {
                if (i == pwdRegList.size() - 1) {
                    isContainSpecialSymbol = true;
                }
                hitTheTargetCount++;
            }
        }
        if (hitTheTargetCount >= 3) {
            return isContainSpecialSymbol ? EnumPwdLevel.High : EnumPwdLevel.Middle;
        } else {
            return EnumPwdLevel.Low;
        }
    }

    /**
     * 验证密码是否通过正则
     *
     * @param pwd 密码明文
     */
    public static boolean validRegPwdRB(String pwd) {
        EnumPwdLevel enumPwdLevel = PasswordUtils.validRegPwdRE(pwd);
        return enumPwdLevel != EnumPwdLevel.Low;
    }

    /**
     * 随机生成用户密码
     *
     * @param realName 用户性名
     * @param mobile   用户手机号
     */
    public static String generateUserPwd(
            String realName,
            String mobile
    ) {
        StringBuffer sb = new StringBuffer();
        String upStr = HanYuPinyinUtils.getInstance().getFirstLettersUp(realName);
        String lowStr = null;
        if (realName.trim().length() == 1) {
            lowStr = upStr.toLowerCase();
        } else {
            lowStr = upStr.substring(1).toLowerCase();
        }
        sb.append(upStr.substring(0, 1));
        sb.append(lowStr);
        sb.append("@");
        sb.append(mobile);
        return sb.toString();
    }
}
